package com.google.wave.poppy.mail;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.apache.tools.ant.taskdefs.SendEmail;

public class TestMail {
	public static String testMail(String IGNORED) {
//		String serverInfo = getServletContext().getServerInfo();
//		String userAgent = getThreadLocalRequest().getHeader("User-Agent");

		MTA mta = MTA.get();
//		MimeMessage outbound = mta.newMimeMessageToSubjBody("updatepoppy+911@gmail.com", "Test @ " + new Date(), "Body test @ " + new Date());
//		MimeMessage outbound = mta.newMimeMessageToSubjBody("hostmaster+911@syncstr.com", "Test @ " + new Date(), "Body test @ " + new Date());
		boolean success = SendMail.sendEmail("WAVEID-test", "elliston@gmail.com", "Test subject from bot", "Body from bot");
		String result = success ? "Message sent successfully" : "message delivery failed";

		Map<String,List<Message>> messages = MTA.get().receiveMessages();
		return result + "\n" + getMessagePrintable(messages);
	}

	public static String getMessagePrintable(Map<String,List<Message>> idToMessageList) {
		StringBuffer sb = new StringBuffer();
		int cMsg = 0;
		for (List<Message> listMessages: idToMessageList.values()) {
			for (Message msg : listMessages) {
				cMsg++;
//				sb.append("WaveId: " + msg.getWaveID() + "\n");
//				sb.append("From: " + msg.getSender() + "\n");
//				sb.append("To: " + msg.getRecipient() + "\n");
//				sb.append("Subject: " + msg.getSubject() + "\n");
//				sb.append("Body: \n" + msg.getBody() + "\n");
			}
		}
		return "Found " + cMsg + " messages\n" + sb.toString();
	}

}
